package com.mybatis;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeGenerator {




    /**生成用户**/
    String author = "likai";


    String parent = "com.autogenerator";

    /**
     * model 模块
     */

    String modelName = "student";

    /**
     * 需要生成的表模型
     */
    private static String[] tableNames = new String[]{
            "sys_orgnization_v2"

    };



    /**
     * 表前缀
     */
    private static String[] tablePrefixs = new String[]{


    };

    /**
     * 字段前缀
     */
    private static String[] fieldPrefixs = new String[]{

    };


    //数据库链接
    private static String sqlUrl = "jdbc:mysql://10.0.0.85:3306/glzj_udms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    private static String sqlUserName = "root";
    private static String sqlPassword = "Glkj@2020";
    private static String driverName = "com.mysql.cj.jdbc.Driver";

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */


    public void codeGenMain() {
        // 代码生成器
        AutoGenerator ag = new AutoGenerator();

        globalConfig(ag);
        templateConfig(ag);
        strategyConfig(ag);
        packageConfig(ag);
        dataSourceConfig(ag);
        ag.execute();
    }

    public static void main(String[] args) {
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.codeGenMain();
    }


    /**
     * 模版设置
     */
    public void templateConfig(AutoGenerator ag) {
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        templateConfig.setEntity("templates/entity.java");
        templateConfig.setController("templates/controller.java");
        templateConfig.setMapper("templates/mapper.java");
        templateConfig.setService("templates/service.java");
        templateConfig.setServiceImpl("templates/serviceImpl.java");
        ag.setTemplate(templateConfig);
    }


    /**
     * 全局设置
     */
    public void globalConfig(AutoGenerator ag) {
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor(author);

        globalConfig.setFileOverride(true);
        globalConfig.setOpen(false);//是否打开输出目录
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setActiveRecord(false);
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setBaseResultMap(true);// XML  base ResultMap
        globalConfig.setBaseColumnList(true);// XML base columList

        globalConfig.setEntityName("%sEntity");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        ag.setGlobalConfig(globalConfig);
    }

    /**
     * 包设置
     */
    public void packageConfig(AutoGenerator ag) {
        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(modelName);
        packageConfig.setParent(parent);
        ag.setPackageInfo(packageConfig);
    }


    /**
     * 数据源设置
     *
     * @param ag
     */
    public void dataSourceConfig(AutoGenerator ag) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(sqlUrl);
        // dsc.setSchemaName("public");
        dataSourceConfig.setDriverName(driverName);
        dataSourceConfig.setUsername(sqlUserName);
        dataSourceConfig.setPassword(sqlPassword);
        ag.setDataSource(dataSourceConfig);
    }


    /**
     * 表设置
     *
     * @param ag
     */
    public void strategyConfig(AutoGenerator ag) {
        StrategyConfig strategy = new StrategyConfig();//数据库表设置
        strategy.setTablePrefix(tablePrefixs);//表前缀
        strategy.setFieldPrefix(fieldPrefixs);//字段前缀


        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntityTableFieldAnnotationEnable(false);//是否生成实体类时生成注解字段
        strategy.setEntitySerialVersionUID(true);
        strategy.setRestControllerStyle(true);

        strategy.setInclude(tableNames);
        strategy.setControllerMappingHyphenStyle(true);
        ag.setStrategy(strategy);
    }
}